﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Notes.Resources;
using System.IO;
using System.Diagnostics;
using System.Threading;

using Microsoft.Phone.Notification;
using Microsoft.Phone.Tasks;
using System.Xml.Serialization;
using NotesAgent;

namespace Notes
{
    public partial class NotesPage : PhoneApplicationPage
    {
        public static List<Ads> ads;
        public static Account currentAccount = new Account();
        public static Result result = new Result();

        public NotesPage()
        {
            /// Holds the push channel that is created or found.

            PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
            InitializeComponent();
            DataContext = App.ViewModel;
            Ads_List.DataContext = App.ViewModel;
            Notes_List.DataContext = App.ViewModel;
            ads = Storage.LoadAds();
            App.ViewModel.Ads.Clear();
            for (int i = 0; i < ads.Count; i++)
                App.ViewModel.Ads.Add(new AdsViewModel()
                {
                    ID = ads[i].ID,
                    Description = ads[i].Description,
                    ImageUri = new Uri(ads[i].ImageUrl, UriKind.RelativeOrAbsolute),
                    Name = ads[i].Name
                }
                );



            BuildApplicationBar();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            RebuildList();

        }
        
        public void RebuildList()
        {
            App.ViewModel.Notes.Clear();
            for (int i = 0; i < result.notes.Count; i++)
                App.ViewModel.Notes.Add(new NoteViewModel()
                {
                    Title = result.notes[i].Title,
                    Modified = result.notes[i].Modified,
                    Item = result.notes[i]
                });

            /*var tiles = ShellTile.ActiveTiles.ToList();

            for (int i = 1; i < tiles.Count; i++)
            {
                bool found = false;
                for(int j=0;j<result.notes.Count; j++)
                {
                    if (tiles[i].NavigationUri.LocalPath.Contains("id=" + result.notes[j].Id))
                    //if (tiles[i].Id == "" + result.notes[j].Id)
                    {
                        found = true;
                        StandardTileData data = new StandardTileData();
                        data.Title = result.notes[j].Title;
                        data.BackContent = result.notes[j].Modified;
                        tiles[i].Update(data);
                    }
                }
                if (!found)
                    tiles[i].Delete();
            }*/

        }
        
        
        public void BuildApplicationBar()
        {
            ApplicationBar.Buttons.Clear();
            ApplicationBarIconButton credits = new ApplicationBarIconButton() { Text = Strings.Credits, IsEnabled = true };
            if (App.CurrentTheme == App.Theme.Dark)
                credits.IconUri = new Uri("\\Images\\dark\\appbar.questionmark.rest.png", UriKind.Relative);
            else
                credits.IconUri = new Uri("\\Images\\light\\appbar.questionmark.rest.png", UriKind.Relative);
            credits.Click += OpenCredits_Button_Click;


            ApplicationBarIconButton update = new ApplicationBarIconButton() { Text = Strings.Update, IsEnabled = true };
            if (App.CurrentTheme == App.Theme.Dark)
                update.IconUri = new Uri("\\Images\\dark\\appbar.update.rest.png", UriKind.Relative);
            else
                update.IconUri = new Uri("\\Images\\light\\appbar.update.rest.png", UriKind.Relative);
            update.Click += UpdateButton_Click;

            ApplicationBarIconButton settings = new ApplicationBarIconButton() { Text = Strings.Settings, IsEnabled = true };
            if (App.CurrentTheme == App.Theme.Dark)
                settings.IconUri = new Uri("\\Images\\dark\\appbar.settings.rest.png", UriKind.Relative);
            else
                settings.IconUri = new Uri("\\Images\\light\\appbar.settings.rest.png", UriKind.Relative);
            settings.Click += OpenSettings_Button_Click;

            ApplicationBarIconButton newnote = new ApplicationBarIconButton() { Text = Strings.New, IsEnabled = true };
            if (App.CurrentTheme == App.Theme.Dark)
                newnote.IconUri = new Uri("\\Images\\dark\\appbar.add.rest.png", UriKind.Relative);
            else
                newnote.IconUri = new Uri("\\Images\\light\\appbar.add.rest.png", UriKind.Relative);
            newnote.Click += NewNote_Button_Click;

            ApplicationBar.Buttons.Add(newnote);
            ApplicationBar.Buttons.Add(credits);
            ApplicationBar.Buttons.Add(settings);
        }

        

        private void OpenCredits_Button_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/CreditsPage.xaml", UriKind.Relative));
        }
        private void UpdateButton_Click(object sender, EventArgs e)
        {

        }
        private void OpenSettings_Button_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.Relative));
        }
        private void NewNote_Button_Click(object sender, EventArgs e)
        {
            NotePage.SelectedNote = new note();
            NavigationService.Navigate(new Uri("/NotePage.xaml", UriKind.Relative));
        }
        private void Ads_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Ads_List.SelectedItem == null)
                return;
            AdsViewModel ads = Ads_List.SelectedItem as AdsViewModel;
            Ads_List.SelectedItem = null;
            if (ads.Name != Strings.OtherProducts && ads.ID != "")
                OpenMarket(ads.ID);


        }
        private void OpenMarket(string ID)
        {

            MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
            marketplaceDetailTask.ContentIdentifier = ID;
            marketplaceDetailTask.Show();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }



        private void Notes_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Notes_List.SelectedItem == null)
                return;
            NoteViewModel note = Notes_List.SelectedItem as NoteViewModel;
            Ads_List.SelectedItem = null;
            NotePage.SelectedNote = note.Item;
            NavigationService.Navigate(new Uri("/NotePage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainPage.skip_update = true;
        }
    }


}